package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.domain.MemberReadHistory;
import com.macro.mall.dto.OmsOrderDetail;
import com.macro.mall.mapper.OmsOrderSettingMapper;
import com.macro.mall.model.OmsOrderSetting;
import com.macro.mall.model.UmsIntegrationChangeHistory;
import com.macro.mall.model.UmsMember;
import com.macro.mall.service.UmsMemberService;
import com.macro.mall.wx.service.GetQrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/member")
public class UmsMemberController {

    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private OmsOrderSettingMapper orderSettingMapper;
    @Autowired
    private GetQrCodeService getQrCodeService;

    /**
     * 显示所有用户，根据条件查询
     * @author 高强
     * @Date 2020年9月4日
     * @param umsMember 包含用户名模糊查询，是否会员，普通用户还是分销员
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMemberList", method = RequestMethod.POST)
    public CommonResult<List<UmsMember>> getUmsMemberList(@RequestBody UmsMember umsMember,
                                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        return memberService.getMemberList(umsMember,pageSize,pageNum);
    }

//    /**
//     * 分销员绑定门店
//     * @author 高强
//     * @Date 2020年9月4日
//     * @param memberId 分销员id
//     * @param storeId 门店id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/bindingStore", method = RequestMethod.POST)
//    public CommonResult bindingStore(@RequestParam(value = "memberId") long memberId,@RequestParam(value = "storeId")long storeId){
//        return memberService.bindingStore(memberId,storeId);
//    }
//
//    /**
//     * 创建分销员
//     * @author 高强
//     * @Date 2020年9月4日
//     * @param umsMember
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/create", method = RequestMethod.POST)
//    public CommonResult createDistributionMember(@RequestBody UmsMember umsMember){
//        return memberService.createDistributionMember(umsMember);
//    }
//
//
//
//    /**
//     * 删除分销员
//     * @author 高强
//     * @Date 2020年9月4日
//     * @param id
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
//    public CommonResult deleteDistributionMember(@PathVariable(value = "id") long id){
//        return memberService.deleteDistributionMember(id);
//    }
//
//    /**
//     * 批量删除分销员
//     * @author 高强
//     * @Date 2020年9月4日
//     * @param ids
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/deleteList", method = RequestMethod.GET)
//    public CommonResult deleteDistributionMember(@RequestParam(value = "ids") List<Long> ids){
//        return memberService.deleteDistributionMemberList(ids);
//    }

    /**
     * 根据买家用户id显示显示用户详情
     * @author 高强
     * @Date 2020年9月8日
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMember/{id}", method = RequestMethod.GET)
    public CommonResult<UmsMember> getMemberById(@PathVariable Long id){
        return memberService.getMemberById(id);
    }

    /**
     * 根据手机号获取用户信息
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMemberByPhone", method = RequestMethod.GET)
    public CommonResult<UmsMember> getMemberById(@RequestParam String phone){
        return memberService.getMemberByPhone(phone);
    }

    /**
     * 查看客户下单情况
     * @param memberId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderByMember", method = RequestMethod.GET)
    public CommonResult<CommonPage<OmsOrderDetail>> getOrderByMember(@RequestParam Long memberId,
                                                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        return memberService.getOrderByMember(memberId, pageNum, pageSize);
    }

    /**
     * 查看客户积分表动情况
     * @param memberId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIntegrationHistory", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsIntegrationChangeHistory>> getIntegrationHistoryByMember(@RequestParam Long memberId,
                                                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        return memberService.getIntegrationHistoryByMember(memberId, pageNum, pageSize);
    }

    /**
     * 查看客户浏览商品情况
     * @param memberId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getReadProductHistory", method = RequestMethod.GET)
    public CommonResult<CommonPage<MemberReadHistory>> getReadProductHistoryByMember(@RequestParam Long memberId,
                                                                               @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                               @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page<MemberReadHistory> readProductHistoryByMember = memberService.getReadProductHistoryByMember(memberId, pageNum, pageSize);
        CommonPage<MemberReadHistory> result = new CommonPage<>();
        result.setPageNum(readProductHistoryByMember.getPageable().getPageNumber()+1);
        result.setPageSize(readProductHistoryByMember.getPageable().getPageSize());
        result.setTotal(readProductHistoryByMember.getTotalElements());
        result.setTotalPage(readProductHistoryByMember.getTotalPages());
        result.setList(readProductHistoryByMember.getContent());
        return CommonResult.success(result);
    }

    /**
     * 经纪人的佣金订单列表
     * @param memberId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCommissionHistory", method = RequestMethod.GET)
    public CommonResult<CommonPage<OmsOrderDetail>> getCommissionHistory(@RequestParam Long memberId,
                                                                         @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        return memberService.getCommissionHistory(memberId, pageNum, pageSize);
    }
    /**
     * 获取经纪人二维码
     * @param page
     * @return
     * @author zhouboyu
     * @Date 2020年9月13日
     */
    @GetMapping(value = "/getQrCode")
    @ResponseBody
    public CommonResult<String> getQrCode(String page,Long memberId) {
        OmsOrderSetting omsOrderSetting = this.orderSettingMapper.selectByPrimaryKey(1L);
        byte[] qrCode = this.getQrCodeService.getQrCode(memberId.toString(), page, omsOrderSetting.getAccessToken());
        return CommonResult.success((new sun.misc.BASE64Encoder()).encode(qrCode));
    }
}
